<?php
require_once '../device/mysql.php';
class location_info extends MySQL
{
    # 连接Mysql数据库
    function connect()
    {
        $conn = parent::connect();
        return $conn;
    }

    #查询最后一个位置
    public function last_location($tableID)
    {
        $conn = $this->connect();
        $table = $tableID . '_location';
        // 执行查询的代码
        $sql = "SELECT Latitude,Longitude FROM $table ORDER BY date DESC LIMIT 1;";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            // 获取第一条记录
            $row = $result->fetch_assoc();
            // 输出记录
            return $row["Latitude"] . ',' . $row["Longitude"];
        } else { //无记录时自动设置中心区域，应该为注册所在城市地
            return "29.350349379999997,106.52152327499999";
        }
        $conn->close();
    }
}
//分割处理函数
function format_coordinates($input)
{
    $parts = explode("|", $input);
    $result = array();
    foreach ($parts as $part) {
        $result[] = "[" . $part . "]";
    }
    return implode(",", $result);
}
//对名称遍历获取返回拼接的字符串

function for_name($arr)
{
    foreach ($arr as $element) {
        echo $element . "";
    }
}

$location = new location_info();
if (isset($_GET['tableID'])) {
    $Long_lat = $location->last_location($_GET['tableID']);

    echo <<<EOF
var clickListener, map = new AMap.Map("container", {
    rotateEnable: true,
    pitchEnable: true,
    zoom: 19,
    pitch: 50,
    rotation: -15,
    viewMode: '3D', //开启3D视图,默认为关闭 
    zooms: [2, 20],
    center: [$Long_lat],
    mapStyle: 'amap://styles/fresh', //设置地图的显示样式
});
var flag = true;
EOF;
    //循环坐标的添加
    $conn = $location->connect();
    $tit = array();
    $table = $_GET['tableID'] . '_risk_district';
    $sql = "SELECT * FROM $table";
    $ret = $conn->query($sql);
    if ($ret->num_rows > 0) {
        //定义节点坐标组
        $i = 1;
        while ($row = $ret->fetch_assoc()) {
            $Longitude_latiude = format_coordinates($row['Longitude_latiude']);
            $name = 'polygon' . $i++;
            echo 'var ' . $name . '=[' . $Longitude_latiude . '];';
            echo <<<PATH
        var $name = new AMap.Polygon({
            path: $name
        });
PATH;
            $tit[] = $name;
        }
        //对地图进行添加
        echo 'map.add([' . implode(',', $tit) . ']);';
        //设置地图视野
        echo 'map.setFitView();';
        //实例化多边形编辑器
        echo 'var polyEditor = new AMap.PolygonEditor(map);';
        echo 'polyEditor.addAdsorbPolygons([' . implode(',', $tit) . ']);';
    } else {
        echo <<<EOF
        var testpath=[[106.527281,29.345974],[106.527281,29.345974],[106.527281,29.345974]];
        var test = new AMap.Polygon({
            path: testpath
        });
        map.add([test]);
        map.setFitView();
        var polyEditor = new AMap.PolygonEditor(map);
        polyEditor.addAdsorbPolygons([test]);//添加事件
EOF;
    }
    echo <<<EOF
//添加事件
polyEditor.on('add', function (data) {
    //console.log(data);
    var polygon = data.target;
    polyEditor.addAdsorbPolygons(polygon);
    //设置节点的双击事件
    polygon.on('dblclick', () => {
        polyEditor.setTarget(polygon);
        polyEditor.open();
    })
})
//完成编辑
polyEditor.on('end', function () {
    var Longitude_latiude=polyEditor.getTarget();
    console.log("打印坐标" + Longitude_latiude);
    save_update(String(Longitude_latiude).replace(/;/g, "|"));
})

//设置节点的双击事件
/*
polygon1.on('dblclick', () => {
    //
    polyEditor.setTarget(polygon1);
    polyEditor.open();
})
//设置节点的双击事件
polygon2.on('dblclick', () => {
    polyEditor.setTarget(polygon2);
    polyEditor.open();
})
*/
//新建预警
function createPolygon() {
    polyEditor.close();
    polyEditor.setTarget();
    polyEditor.open();
    flag = true;
}
function save_update(data) {
    //页面层-自定义
    if (flag) {
        flag = false;
        var table_id = '1007';
        var Longitude_latiude = data;
        layer.open({
            type: 2,
            title: '新建预警区域',
            maxmin: true,
            skin: 'layui-layer-lan',
            shadeClose: true, //点击遮罩关闭层
            area: ['400px', '280px'],
            content: '../device/AddRDInfo.php',//弹框显示的url
            resize: false,
            maxmin: false, //开启最大化最小化按钮
            success: function (layero, index, that) {
                layer.iframeAuto(index); // 让 iframe 高度自适应
                that.offset(); // 重新自适应弹层坐标
                // 获取子页面的iframe
                //var body = layer.getChildFrame('body', index);
                //body.contents().find("#name").val(table_id);  // #detailId  子页面元素id
                //通过索引获取到当前iframe弹出层
                var iframe = window['layui-layer-iframe' + index];
                //调用iframe弹出层内的方法
                iframe.edit(table_id, Longitude_latiude);

            }
        });
    }
}
//设置目标
//打开编辑器
polyEditor.open();
EOF;
} else {
    echo "参数错误";
}
